<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.searchKey" :placeholder="$t('table.search')" clearable style="width: 90px" class="filter-item">
        <el-option v-for="item in searchOptions" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-input v-model="listQuery.searchValue" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"/>
      <!--<el-select v-model="listQuery.is_available" :placeholder="$t('table.available')" clearable style="width: 90px" class="filter-item">-->
      <!--<el-option v-for="(item, index) in availableOptions" :key="index" :label="item.label" :value="item.value"/>-->
      <!--</el-select>-->
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('table.search') }}</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-share" @click="handleCreate">{{ $t('table.add') }}</el-button>
    </div>
    <el-table
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%">
      <el-table-column
        :label="$t('table.id')"
        align="center"
        width="50">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column
        :label="$t('table.available')"
        align="center"
        width="100px">
        <template slot-scope="scope">
          <span>{{ scope.row.available_name }}</span>
        </template>
      </el-table-column>

      <el-table-column
        :label="$t('usercouponfiles.is_used')"
        align="center"
        width="100px">
        <template slot-scope="scope">
          <span>{{ scope.row.status_name }}</span>
        </template>
      </el-table-column>

      <el-table-column
        :show-overflow-tooltip="true"
        :label="$t('usercouponfiles.coupon_id')"
        align="center"
        width="150px">
        <template slot-scope="scope">
          <span>{{ scope.row.coupon_name }}</span>
        </template>
      </el-table-column>

      <el-table-column
        :label="$t('usercouponfiles.user_id')"
        align="center"
        min-width="160px">
        <template slot-scope="scope">
          <span>{{ scope.row.user_name }}</span>
        </template>
      </el-table-column>

      <el-table-column
        :label="$t('table.start_date')"
        align="center"
        min-width="160px">
        <template slot-scope="scope">
          <span>{{ scope.row.start_date }}</span>
        </template>
      </el-table-column>

      <el-table-column
        :label="$t('table.end_date')"
        align="center"
        min-width="100px">
        <template slot-scope="scope">
          <span>{{ scope.row.end_date }}</span>
        </template>
      </el-table-column>

      <el-table-column
        :label="$t('table.used_time')"
        align="center"
        min-width="100px">
        <template slot-scope="scope">
          <span>{{ scope.row.used_time }}</span>
        </template>
      </el-table-column>

      <el-table-column
        :label="$t('table.actions')"
        align="center"
        width="230">
        <template slot-scope="scope">
          <!--<el-button type="primary" size="small" class="el-icon-edit" @click="handleUpdate(scope.row.id)">{{ $t('table.edit') }}</el-button>-->
          <el-button type="danger" size="small" class="el-icon-delete" @click="destroyRecord(scope.row)">{{ $t('table.delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="indexRecord" />

    <detail ref="detail" :is-edit="isEdit" :edit-id="editId" @indexRecord="indexRecord"/>

  </div>
</template>

<script>
import usercouponApi from '@/api/coupon/usercoupon'
import Detail from './components/Detail'

import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import i18n from '@/lang/index'

export default {
  name: 'Tag',
  components: { Pagination, Detail },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        lang: i18n.locale,
        page: 1,
        limit: 20,
        is_available: 2,
        name: '',
        sort: 'desc',
        orderby: 'id',
        searchKey: 'user_id',
        searchValue: ''
      },
      availableOptions: [
        { 'label': i18n.t('table_element.available.all'), 'value': 2 },
        { 'label': i18n.t('table_element.available.on'), 'value': 1 },
        { 'label': i18n.t('table_element.available.un'), 'value': 0 }
      ],
      editId: 0,
      isEdit: false,
      searchOptions: [
        { value: 'user_id', label: i18n.t('usercouponfiles.user_id') },
        { value: 'coupon_id', label: i18n.t('usercouponfiles.coupon_id') }
      ]
    }
  },
  created() {
    this.indexRecord()
  },
  methods: {

    handleFilter() {
      this.listQuery.page = 1
      this.indexRecord()
    },

    handleCreate() {
      this.$refs.detail.handShowDetailDialog(0)
    },

    handleUpdate(id) {
      this.$refs.detail.handShowDetailDialog(id)
    },
    indexRecord() {
      this.listLoading = true
      usercouponApi.indexRecord(this.listQuery).then(response => {
        const serviceResponse = response.data
        if (serviceResponse.code === 0) {
          this.list = serviceResponse.data.data
          this.total = serviceResponse.data.total
        }
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },

    destroyRecord(row) {
      this.$confirm('Confirm deletion?', 'Message', {
        confirmButtonText: 'Confirm',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(() => {
        usercouponApi.destroyRecord(row.id).then(response => {
          const serviceResponse = response.data
          if (serviceResponse.code === 0) {
            this.$message({
              type: 'success',
              message: 'success!'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          } else {
            this.$message({
              type: 'error',
              message: 'error!'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: ' Canceled deletion'
        })
      })
    }
  }
}
</script>
